﻿using log4net;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SimulationDrawingGDI
{
    public partial class SimulationDrawing : Form
    {
        private static ILog _log = LogManager.GetLogger(typeof(SimulationDrawing));
        /// <summary>
        /// 这个程序描述GDI怎么画图，我尽量用最简洁的语句来表达算法
        /// 在没有单片机数据传入的情况下写一个简单的界面
        /// 横坐标就不需要做动态跟踪时间轴演示
        /// 有详细需要的朋友联系小编找到我
        /// 心电图、仪表需要单片机提供接口
        /// </summary>
        public SimulationDrawing()
        {
            InitializeComponent();
            _log.Info("初始化图表完成");
            panel1.BackColor = Color.White;
            timer1.Enabled = false;
        }
        /// <summary>
        /// 开始
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
        }      
        Graphics g; //画图对象                   
        public int yaa = 240; //写数据竖坐标初始位置

        //申明两个随机坐标
        public int ya = 150; 
        public int xa = 80;

        public int ya1 = 150;
        public int xa1 = 80;

        //外部申明画笔和画刷工具
        Pen px = new Pen(Color.SlateBlue, 1);
        Font myFont = new Font("宋体", 9, FontStyle.Bold);
        Brush bush = new SolidBrush(Color.Black);

        /// <summary>
        /// 计数器
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            //给横坐标一个阈值
            if (xa < panel1.Width - 40)
            {
                //画第一条线随机产生数值
                g = this.panel1.CreateGraphics();
                int iRnd = System.DateTime.Now.Millisecond;
                Random ran = new Random(iRnd);
                int RandKey = ran.Next(0, 200);
                Pen p = new Pen(Color.Red, 2);
                g.DrawLine(p, xa, ya, xa + 10, RandKey);
                xa = xa + 10;
                ya = RandKey;

                //画第二条线随机产生数值
                g = this.panel1.CreateGraphics();
                Random r = new Random();
                int Ra = r.Next(10, 100);
                Pen p1 = new Pen(Color.BlueViolet, 2);
                g.DrawLine(p1, xa1, ya1, xa1 + 10, Ra);
                xa1 = xa1 + 10;
                ya1 = Ra;

                //画两个随机数值表格
                int me = Convert.ToInt32(xa / 60);
                textBox1.Text = ya.ToString();
                textBox2.Text = me.ToString();
                textBox3.Text = ya1.ToString();
                //党写字换行时，自动初始化竖向位置
                if (xa % 60 == 0)
                {
                    yaa = 240;
                }
                //写一行字，递增20px高度
                yaa = yaa + 20;
                //写随机数
                g.DrawString(ya.ToString() + ",", myFont, bush, 60 * me - 10, yaa);
                g.DrawString(ya1.ToString(), myFont, bush, 60 * me + 20, yaa);
            }
        }
        //初始化图表
        void pant()
        {
            //画竖坐标标尺
            g = panel1.CreateGraphics();
            Pen p = new Pen(Color.Black, 2);
            Pen p1 = new Pen(Color.SkyBlue, 1);
            int wid = 15;
            for (int i = 0; i < 200; i = i + 5)
            {
                if ((i + 5) % 40 == 0)
                {
                    wid = 30;
                    g.DrawString((i + 5).ToString(), myFont, bush, 5, i - 12);

                }
                else
                {
                    wid = 15;
                }
                g.DrawLine(p, 40 - wid, i + 5, 40, i + 5);
                g.DrawLine(p1, 40, i, panel1.Width - 40, i);
            }

            //画竖坐标线
            g.DrawLine(p, 40, 0, 40, 200);

            //画横坐标标尺
            for (int j = 0; j < Convert.ToInt32(panel1.Width / 60); j++)
            {

                g.DrawString(j.ToString(), myFont, bush, j * 60 + 35, 210);

                g.DrawLine(p, j * 60 + 40, 190, j * 60 + 40, 205);
                g.DrawLine(p1, j * 60 + 40, 0, j * 60 + 40, 205);


                g.DrawLine(px, 40, 240, 40, 360);
                g.DrawLine(px, j * 60 + 40, 240, j * 60 + 40, 380);
            }

            //画横坐标线
            g.DrawLine(p, 0, 200, panel1.Width, 200);
        }
        /// <summary>
        /// 停止初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            g.Clear(Color.White);
            xa = 80;
            ya = 150;
            xa1 = 80;
            ya1 = 150;
            timer1.Enabled = false;
            pant();
            yaa = 240;
        }
        /// <summary>
        /// 启动重绘
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SimulationDrawing_Paint(object sender, PaintEventArgs e)
        {
            pant();
        }
        /// <summary>
        /// 暂停
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
        }
    }
}
